package com.lswms.modules.lswms.service.impl.orders;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.page.PageData;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.common.utils.Result;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.Constant.OrderConstant;
import com.lswms.modules.lswms.Constant.TaskConstant;
import com.lswms.modules.lswms.Constant.TradeConstant;
import com.lswms.modules.lswms.dao.base.LocationDao;
import com.lswms.modules.lswms.dao.orders.OrderallocationDao;
import com.lswms.modules.lswms.dao.orders.OrderdetailDao;
import com.lswms.modules.lswms.dao.orders.OrderdetaillistDao;
import com.lswms.modules.lswms.dao.orders.OrdersDao;
import com.lswms.modules.lswms.dao.stock.InventoryDao;
import com.lswms.modules.lswms.dao.stock.TradeDao;
import com.lswms.modules.lswms.dao.task.TaskDao;
import com.lswms.modules.lswms.dao.uniquecode.UniquecodeDao;
import com.lswms.modules.lswms.dao.uniquecode.UniquecoderecordDao;
import com.lswms.modules.lswms.dto.orders.OrderdetaillistDTO;
import com.lswms.modules.lswms.entity.base.LocationEntity;
import com.lswms.modules.lswms.entity.orders.OrderdetailEntity;
import com.lswms.modules.lswms.entity.orders.OrderdetaillistEntity;
import com.lswms.modules.lswms.entity.orders.OrdersEntity;
import com.lswms.modules.lswms.entity.stock.InventoryEntity;
import com.lswms.modules.lswms.entity.stock.TradeEntity;
import com.lswms.modules.lswms.entity.task.TaskEntity;
import com.lswms.modules.lswms.entity.uniquecode.UniquecodeEntity;
import com.lswms.modules.lswms.entity.uniquecode.UniquecoderecordEntity;
import com.lswms.modules.lswms.service.base.SequenceService;
import com.lswms.modules.lswms.service.impl.uniquecode.UniquecodeServiceImpl;
import com.lswms.modules.lswms.service.orders.OrderdetaillistService;
import com.lswms.modules.lswms.service.receipt.ReceiptdetaillistService;

/**
 * 出库明细记录
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-06-24
 */
@Service
public class OrderdetaillistServiceImpl
		extends CrudServiceImpl<OrderdetaillistDao, OrderdetaillistEntity, OrderdetaillistDTO>
		implements OrderdetaillistService {

	@Autowired
	InventoryDao inventoryDao;

	@Autowired
	OrderdetailDao orderdetailDao;

	@Autowired
	OrdersDao ordersDao;

	@Autowired
	TaskDao taskDao;

	@Autowired
	SequenceService sequenceService;

	@Autowired
	TradeDao tradeDao;

	@Autowired
	OrderallocationDao orderallocationDao;

	@Autowired
	ReceiptdetaillistService receiptdetaillistService;

	@Autowired
	private UniquecodeServiceImpl uniquecodeService;

	@Autowired
	private LocationDao locationDao;
	
	@Autowired
	private UniquecodeDao uniquecodeDao ;
	
	@Autowired
	private UniquecoderecordDao uniquecoderecordDao;

	@Override
	public QueryWrapper<OrderdetaillistEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");
		String orderkey = (String) params.get("orderkey");
		String line = (String) params.get("line");
		String linelist = (String) params.get("linelist");
		String sku = (String) params.get("sku");
		String descr = (String) params.get("descr");
		String status = (String) params.get("status");

		QueryWrapper<OrderdetaillistEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "lswms_orderdetaillist.id", id);
		wrapper.eq(StringUtils.isNotBlank(orderkey), "lswms_orderdetaillist.orderkey", orderkey);
		wrapper.eq(StringUtils.isNotBlank(line), "lswms_orderdetaillist.line", line);
		wrapper.eq(StringUtils.isNotBlank(linelist), "lswms_orderdetaillist.linelist", linelist);
		wrapper.eq(StringUtils.isNotBlank(sku), "lswms_orderdetaillist.sku", sku);
		wrapper.like(StringUtils.isNotBlank(descr), "lswms_sku.descr", descr);
		wrapper.eq(StringUtils.isNotBlank(status), "lswms_orderdetaillist.status", status);

		wrapper.orderByDesc("orderkey");
		wrapper.orderByAsc("line");
		wrapper.orderByAsc("linelist");

		return wrapper;
	}

	@Override
	public PageData<OrderdetaillistEntity> pageBySql(Map<String, Object> params) {
		IPage<OrderdetaillistEntity> page = baseDao.selectPageBySql(getPage(params, null, false), getWrapper(params));
		return getPageData(page, OrderdetaillistEntity.class);
	}
	
	
	@Override
	public List<OrderdetaillistEntity> listBySql(Map<String, Object> params) {
		List<OrderdetaillistEntity> list =baseDao.listBySql( getWrapper(params));
		return list;
	}


	@Override
	public PageData<OrderdetaillistEntity> pageListBySql(Map<String, Object> params) {
		
		String orderkey = (String) params.get("orderkey");
		String line = (String) params.get("line");
		String linelist = (String) params.get("linelist");
		String sku = (String) params.get("sku");
		String descr = (String) params.get("descr");
		String status = (String) params.get("status");
		String recipientId = (String) params.get("recipientId");
		String recipient = (String) params.get("recipient");
		String sort = (String) params.get("sort");
		String factorycdoe = (String) params.get("factorycdoe");
		String lot7 = (String) params.get("lot7");
		String lot6 = (String) params.get("lot6");

		String[] sorts = null;
		if (StringUtils.isNotBlank(sort)) {
			sorts = sort.split("#");
		}
		
		QueryWrapper<OrderdetaillistEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(orderkey), "odl.orderkey", orderkey);
		wrapper.eq(StringUtils.isNotBlank(line), "odl.line", line);
		wrapper.eq(StringUtils.isNotBlank(linelist), "odl.linelist", linelist);
		wrapper.eq(StringUtils.isNotBlank(sku), "odl.sku", sku);
		wrapper.like(StringUtils.isNotBlank(descr), "lswms_sku.descr", descr);
		wrapper.eq(StringUtils.isNotBlank(status), "odl.status", status);
		wrapper.eq(StringUtils.isNotBlank(recipientId), "od.recipientId", recipientId);
		wrapper.eq(StringUtils.isNotBlank(recipient), "od.recipient", recipient);
		wrapper.eq(StringUtils.isNotBlank(lot7), "lot.lot7", lot7);
		wrapper.like(StringUtils.isNotBlank(lot6), "lot.lot6", lot6);
		wrapper.like(StringUtils.isNotBlank(factorycdoe), "lswms_sku.factorycdoe", factorycdoe);
		wrapper.in(sorts != null && sorts.length > 0, "lswms_sku.sort", sorts);

		wrapper.orderByDesc("odl.orderkey");
		wrapper.orderByAsc("odl.line");
		wrapper.orderByAsc("odl.linelist");
		
		
		IPage<OrderdetaillistEntity> page = baseDao.selectPageListBySql(getPage(params, null, false), wrapper);
		return getPageData(page, OrderdetaillistEntity.class);
	}
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String saveAllocationByHandle(OrderdetaillistDTO dto) {
		// 校验订单状态，状态是关闭，取消，不能分配
		QueryWrapper<OrdersEntity> orderQuery = new QueryWrapper<OrdersEntity>();
		orderQuery.eq("orderkey", dto.getOrderkey());

		OrdersEntity ordersEntity = ordersDao.selectOne(orderQuery);
		Integer statusOrders = ordersEntity.getStatus();
		if (statusOrders == OrderConstant.ORDERSTATUS_25 || statusOrders == OrderConstant.ORDERSTATUS_30) {
			return "订单已关闭或取消，不能分配";
		}

		BigDecimal orderQty = dto.getQty();
		if (orderQty.compareTo(BigDecimal.ZERO) <= 0) {
			return "拣货明细数量必须为正数";
		}

		//如果是立体库位,校验堆垛机是否可用
		QueryWrapper<LocationEntity> queryLocation = new QueryWrapper<LocationEntity>();
		queryLocation.eq("location", dto.getLoc()) ;
		queryLocation.exists(" select  1 from WCS_DDJ where lswms_location.lane = WCS_DDJ.DVC_NO and WCS_DDJ.status <>'1' ") ;
		
		Integer count1 =locationDao.selectCount(queryLocation);
		if(count1 ==1) {
			return "选择的库位的堆垛机已停用，无法出库";
		}
		
		// 查询库存数据，校验录入的数量是小于或等于所选的库存量
		QueryWrapper<InventoryEntity> inventroyQuery = new QueryWrapper<InventoryEntity>();
		inventroyQuery.eq("sku", dto.getSku());
		inventroyQuery.eq("location", dto.getLoc());
		inventroyQuery.eq("lot", dto.getLot());
		inventroyQuery.eq("tray", dto.getTray());
		inventroyQuery.eq("box", dto.getBox());
		List<InventoryEntity> inventoryList = inventoryDao.selectList(inventroyQuery);

		InventoryEntity inventoryEntity = new InventoryEntity();
		if (inventoryList.size() > 0) {
			inventoryEntity = inventoryList.get(0);
		} else {
			return MEGConstant.INVENTORY_NOT_EXIST;
		}

		// 对比数量
		BigDecimal inventoryQty = inventoryEntity.getQty();
		if (orderQty.compareTo(inventoryQty) > 0) {
			return MEGConstant.ORDERQTY_BIGGER_INVENTORYQTY;
		}

		// 所选的货存在其他未完成的任务，不允许分配
		QueryWrapper<TaskEntity> taskQuery = new QueryWrapper<TaskEntity>();
		taskQuery.eq("sku", dto.getSku());
		taskQuery.eq("fromlocation", dto.getLoc());
		taskQuery.eq("fromlot", dto.getLot());
		taskQuery.eq("fromtray", dto.getTray());
		taskQuery.eq("frombox", dto.getBox());
		taskQuery.ne("status", 9);
		Integer taskCount = taskDao.selectCount(taskQuery);
		if (taskCount > 0) {
			return MEGConstant.INVENTORY_HAS_OTHER_TASK;
		}

		// 完善分配的信息
		Integer count = getOrderdetaillistMaxLine(dto.getOrderkey(), dto.getLine());
		dto.setLinelist(count + 1);
		dto.setFromloc(dto.getLoc());
		dto.setFromtray(dto.getTray());
		dto.setFrombox(dto.getBox());
		dto.setToloc(OrderConstant.ORDER_PICK_LOCATION);
		dto.setTotray(dto.getTray());
		dto.setTobox(dto.getBox());
		dto.setStatus(OrderConstant.ORDERSTATUS_5);
		dto.setActqty(dto.getQty());
		save(dto);

		// 更新orderdetail的数据
		QueryWrapper<OrderdetailEntity> orderdetailQuery = new QueryWrapper<OrderdetailEntity>();
		orderdetailQuery.eq("orderkey", dto.getOrderkey());
		orderdetailQuery.eq("line", dto.getLine());
		OrderdetailEntity orderdetailEntity = orderdetailDao.selectOne(orderdetailQuery);
		//List<OrderdetailEntity> orderdetailEntities = orderdetailDao.selectList(orderdetailQuery);
		/*
		 * for (OrderdetailEntity orderdetailEntity :orderdetailEntities ) {
		 * 
		 * }
		 */
		OrderdetailEntity updateOrderdetail = new OrderdetailEntity();
		updateOrderdetail.setAllocateqty(orderdetailEntity.getAllocateqty().add(dto.getActqty()));
		// 计算剩余量
//		BigDecimal qty = orderdetailEntity.getQty();
//		BigDecimal subtract = qty.subtract(updateOrderdetail.getAllocateqty());
//		BigDecimal subtract1 = subtract.subtract(orderdetailEntity.getPickqty());
//		BigDecimal diffQty = subtract1.subtract(orderdetailEntity.getActqty());
//		if (diffQty.compareTo(new BigDecimal(0)) < 0) {
//			diffQty = new BigDecimal(0);
//		}

//		updateOrderdetail.setDiffqty(diffQty);

		UpdateWrapper<OrderdetailEntity> orderdetailUpdate = new UpdateWrapper<OrderdetailEntity>();
		orderdetailUpdate.eq("orderkey", dto.getOrderkey());
		orderdetailUpdate.eq("line", dto.getLine());
		orderdetailDao.update(updateOrderdetail, orderdetailUpdate);

		// 变更库存数据
		InventoryEntity updateInventory = new InventoryEntity();
		updateInventory.setPickqty(inventoryEntity.getPickqty().add(orderQty));

		UpdateWrapper<InventoryEntity> updateInventoryWapper = new UpdateWrapper<InventoryEntity>();
		updateInventoryWapper.eq("id", inventoryEntity.getId());
		updateInventoryWapper.eq("sku", dto.getSku());
		updateInventoryWapper.eq("location", dto.getLoc());
		updateInventoryWapper.eq("lot", dto.getLot());
		updateInventoryWapper.eq("tray", dto.getTray());
		updateInventoryWapper.eq("box", dto.getBox());
		inventoryDao.update(updateInventory, updateInventoryWapper);

		changeOrderStatus(dto.getOrderkey());

		return null;
	}

	/**
	 * @param orderkey 变更订单状态，还不完善
	 */

	public void changeOrderStatus(String orderkey) {
		// 根据拣货明细的状态便跟订单明细的状态
		changeOrderdetailByOrderdetaillist(orderkey);

		// 更新所有明细的状态
		QueryWrapper<OrderdetailEntity> orderdetailQuery = new QueryWrapper<OrderdetailEntity>();
		orderdetailQuery.eq("orderkey", orderkey);
		List<OrderdetailEntity> orderdetailEntitys = orderdetailDao.selectList(orderdetailQuery);
		boolean exists_0 = false;
		boolean exists_3 = false;
		boolean exists_5 = false;
		boolean exists_7 = false;
		boolean exists_9 = false;
		boolean exists_11 = false;
		boolean exists_13 = false;
		boolean exists_14 = false;
		boolean exists_15 = false;
		boolean exists_16 = false;
		boolean exists_17 = false;
		boolean exists_18 = false;
		boolean exists_19 = false;
		boolean exists_20 = false;
		boolean exists_21 = false;
		boolean exists_23 = false;
		boolean exists_25 = false;
		boolean exists_30 = false;

		for (OrderdetailEntity orderdetailEntity : orderdetailEntitys) {

			Integer currentStatus = orderdetailEntity.getStatus();
			if (currentStatus == OrderConstant.ORDERSTATUS_0) {
				exists_0 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_3) {
				exists_3 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_5) {
				exists_5 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_7) {
				exists_7 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_9) {
				exists_9 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_11) {
				exists_11 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_13) {
				exists_13 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_14) {
				exists_14 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_15) {
				exists_15 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_16) {
				exists_16 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_17) {
				exists_17 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_18) {
				exists_18 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_19) {
				exists_19 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_20) {
				exists_20 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_21) {
				exists_21 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_23) {
				exists_23 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_25) {
				exists_25 = true;
			} else if (currentStatus == OrderConstant.ORDERSTATUS_30) {
				exists_30 = true;
			}

//			if (diffQty.compareTo(zero) == 0) {
//			if (actQty.compareTo(zero) > 0) {
//				if (diffQty.compareTo(zero) > 0 || allocateQty.compareTo(zero) > 0 || pickQty.compareTo(zero) > 0) {
//					// 部分发货
//					currentStatus = OrderConstant.ORDERSTATUS_19;
//					exists_19 = true;
//				} else {
//					// 已发货
//					currentStatus = OrderConstant.ORDERSTATUS_21;
//					exists_21 = true;
//				}
//			} else if (pickQty.compareTo(zero) > 0) {
//				if (diffQty.compareTo(zero) > 0 || allocateQty.compareTo(zero) > 0) {
//					// 部分拣选
//					currentStatus = OrderConstant.ORDERSTATUS_11;
//					exists_11 = true;
//				} else {
//					// 已拣选
//					currentStatus = OrderConstant.ORDERSTATUS_13;
//					exists_13 = true;
//				}
//			} else {
//				// 查询发放的量
//				QueryWrapper<OrderdetaillistEntity> OrderdetaillistQuery = new QueryWrapper<OrderdetaillistEntity>();
//				OrderdetaillistQuery.eq("orderkey", orderdetailEntity.getOrderkey());
//				OrderdetaillistQuery.eq("line", orderdetailEntity.getLine());
//				OrderdetaillistQuery.eq("status", 1);
//				OrderdetaillistQuery.select(" isnull( sum(actqty),0) sumQty ");
//				List<Map<String, Object>> sumList = baseDao.selectMaps(OrderdetaillistQuery);
//				BigDecimal sendqty = new BigDecimal(0);
//
//				if (sumList.size() > 0) {
//					sendqty = (BigDecimal) sumList.get(0).get("sumQty");
//				}
//				if (sendqty.compareTo(zero) > 0) {
//					if (diffQty.compareTo(zero) > 0 || allocateQty.compareTo(sendqty) > 0) {
//						// 部分下发
//						currentStatus = OrderConstant.ORDERSTATUS_7;
//						exists_7 = true;
//					} else {
//						// 下发
//						currentStatus = OrderConstant.ORDERSTATUS_9;
//						exists_9 = true;
//					}
//				} else if (diffQty.compareTo(zero) > 0 || allocateQty.compareTo(zero) > 0) {
//					// 部分分配
//					currentStatus = OrderConstant.ORDERSTATUS_3;
//					exists_3 = true;
//				} else {
//					// 已分配
//					currentStatus = OrderConstant.ORDERSTATUS_5;
//					exists_5 = true;
//				}
//			}
////			}
//
//			// 更新状态
//			UpdateWrapper<OrderdetailEntity> orderdetailUpdate = new UpdateWrapper<OrderdetailEntity>();
//			orderdetailUpdate.eq("orderkey", orderdetailEntity.getOrderkey());
//			orderdetailUpdate.eq("line", orderdetailEntity.getLine());
//			orderdetailUpdate.set("status", currentStatus);
//			orderdetailDao.update(null, orderdetailUpdate);

		}

		/**
		 * 出库单状态:新建-0 ,部分分配-3,已分配-5,部分下发-7,下发-9,部分拣选-11,已拣选-13,部分复核-14,已复核-15,
		 * 部分集货-16,集货-17,部分装车-18,已装车-19, 部分发货-20,已发货-21 ,完全发货-23,关闭-25,取消-30
		 */
		// 主表状态
		Integer ordersStatus = OrderConstant.ORDERSTATUS_0;

		if (exists_20) {
			ordersStatus = OrderConstant.ORDERSTATUS_20;
		} else if (exists_21) {
			if (!exists_0 && !exists_3 && !exists_5 && !exists_7 && !exists_9 && !exists_11 && !exists_13 && !exists_14
					&& !exists_15 && !exists_16 && !exists_17 && !exists_18) {
				ordersStatus = OrderConstant.ORDERSTATUS_21;
			} else {
				ordersStatus = OrderConstant.ORDERSTATUS_20;
			}
		} else if (exists_18) {
			ordersStatus = OrderConstant.ORDERSTATUS_18;
		} else if (exists_19) {
			if (!exists_0 && !exists_3 && !exists_5 && !exists_7 && !exists_9 && !exists_11 && !exists_13 && !exists_14
					&& !exists_15 && !exists_16 && !exists_17) {
				ordersStatus = OrderConstant.ORDERSTATUS_19;
			} else {
				ordersStatus = OrderConstant.ORDERSTATUS_18;
			}

		} else if (exists_16) {
			ordersStatus = OrderConstant.ORDERSTATUS_16;
		} else if (exists_17) {
			if (!exists_0 && !exists_3 && !exists_5 && !exists_7 && !exists_9 && !exists_11 && !exists_13 && !exists_14
					&& !exists_15) {
				ordersStatus = OrderConstant.ORDERSTATUS_17;
			} else {
				ordersStatus = OrderConstant.ORDERSTATUS_16;
			}
		} else if (exists_14) {
			ordersStatus = OrderConstant.ORDERSTATUS_14;
		} else if (exists_15) {
			if (!exists_0 && !exists_3 && !exists_5 && !exists_7 && !exists_9 && !exists_11 && !exists_13) {
				ordersStatus = OrderConstant.ORDERSTATUS_15;
			} else {
				ordersStatus = OrderConstant.ORDERSTATUS_14;
			}

		} else if (exists_11) {
			ordersStatus = OrderConstant.ORDERSTATUS_11;
		} else if (exists_13) {
			if (!exists_0 && !exists_3 && !exists_5 && !exists_7 && !exists_9) {
				ordersStatus = OrderConstant.ORDERSTATUS_13;
			} else {
				ordersStatus = OrderConstant.ORDERSTATUS_11;
			}

		} else if (exists_7) {
			ordersStatus = OrderConstant.ORDERSTATUS_7;
		} else if (exists_9) {
			if (!exists_0 && !exists_3 && !exists_5) {
				ordersStatus = OrderConstant.ORDERSTATUS_9;
			} else {
				ordersStatus = OrderConstant.ORDERSTATUS_7;
			}

		} else if (exists_3) {
			ordersStatus = OrderConstant.ORDERSTATUS_3;
		} else if (exists_5) {
			if (!exists_0) {
				ordersStatus = OrderConstant.ORDERSTATUS_5;
			} else {
				ordersStatus = OrderConstant.ORDERSTATUS_3;
			}

		}

		UpdateWrapper<OrdersEntity> ordersUpdate = new UpdateWrapper<OrdersEntity>();
		ordersUpdate.eq("orderkey", orderkey);
		ordersUpdate.set("status", ordersStatus);
		ordersDao.update(null, ordersUpdate);

	}

	private void changeOrderdetailByOrderdetaillist(String orderkey) {
		// 更新所有明细的状态
		QueryWrapper<OrderdetailEntity> orderdetailQuery = new QueryWrapper<OrderdetailEntity>();
		orderdetailQuery.eq("orderkey", orderkey);
		List<OrderdetailEntity> orderdetailEntitys = orderdetailDao.selectList(orderdetailQuery);

		for (OrderdetailEntity orderdetailEntity : orderdetailEntitys) {

			// allocateqty pickqty actqty

			// 差异量
			BigDecimal diffQtyOrderdetail = orderdetailEntity.getDiffqty();
			// 分配量
			BigDecimal allocateQtyOrderdetail = orderdetailEntity.getAllocateqty();
			// 拣货量
			BigDecimal pickQtyOrderdetail = orderdetailEntity.getPickqty();
			// 发货量
			BigDecimal actQtyOrderdetail = orderdetailEntity.getActqty();

			QueryWrapper<OrderdetaillistEntity> orderdetaillistEntityQuery = new QueryWrapper<OrderdetaillistEntity>();
			orderdetaillistEntityQuery.eq("orderkey", orderdetailEntity.getOrderkey());
			orderdetaillistEntityQuery.eq("line", orderdetailEntity.getLine());

			/**
			 * 出库单明细记录状态:新建-0 ,发放-1,已拣货-5,复核-7,集货-9,装车-11 ,已发货-13
			 */

			/*
			 * boolean exists_0 = false; boolean exists_1 = false; boolean exists_5 = false;
			 * boolean exists_7 = false; boolean exists_9 = false; boolean exists_11 =
			 * false; boolean exists_13 = false;
			 */
			boolean exists_0 = false;
			boolean exists_3 = false;
			boolean exists_5 = false;
			boolean exists_7 = false;
			boolean exists_9 = false;
			boolean exists_11 = false;
			boolean exists_13 = false;
			boolean exists_14 = false;
			boolean exists_15 = false;
			boolean exists_16 = false;
			boolean exists_17 = false;
			boolean exists_18 = false;
			boolean exists_19 = false;
			boolean exists_20 = false;
			boolean exists_21 = false;
			boolean exists_23 = false;
			boolean exists_25 = false;
			boolean exists_30 = false;

			BigDecimal actQty = new BigDecimal(0);

			List<OrderdetaillistEntity> list = baseDao.selectList(orderdetaillistEntityQuery);
			for (OrderdetaillistEntity orderdetaillistEntity : list) {
				Integer statusInt = orderdetaillistEntity.getStatus();

				actQty.add(orderdetaillistEntity.getActqty());
				if (statusInt == OrderConstant.ORDERSTATUS_0) {
					exists_0 = true;
				} else if (statusInt == OrderConstant.ORDERSTATUS_5) {
					exists_5 = true;
				} else if (statusInt == OrderConstant.ORDERSTATUS_9) {
					exists_9 = true;
				} else if (statusInt == OrderConstant.ORDERSTATUS_13) {
					exists_13 = true;
				} else if (statusInt == OrderConstant.ORDERSTATUS_21) {
					exists_21 = true;
				}
			}

			/**
			 * 出库单状态:新建-0 ,部分分配-3,已分配-5,部分下发-7,下发-9,部分拣选-11,已拣选-13,
			 * 部分集货-15,集货-17,部分装车-18,已装车-19, 部分发货-20,已发货-21,完全发货-23,关闭-25,取消-30
			 */

			int orderdetailStatus = OrderConstant.ORDERSTATUS_0;

			BigDecimal zero = new BigDecimal(0);
			if (diffQtyOrderdetail.compareTo(zero) > 0) {
				// 差异量>0 发货量>0，分配量=0，拣货量=0 单据状态为部分发货-20
				if (actQtyOrderdetail.compareTo(zero) > 0 && allocateQtyOrderdetail.compareTo(zero) == 0
						&& pickQtyOrderdetail.compareTo(zero) == 0) {
					orderdetailStatus = OrderConstant.ORDERSTATUS_20;
				} else {
					if (exists_5) {
						orderdetailStatus = OrderConstant.ORDERSTATUS_3;
					} else if (exists_13) {
						// 存在拣货，明细的状态为部分拣货
						orderdetailStatus = OrderConstant.ORDERSTATUS_11;
					} else if (exists_9) {
						// 存在发放，明细的状态为部分发放
						orderdetailStatus = OrderConstant.ORDERSTATUS_7;
					}
				}

			} else {
				if (actQtyOrderdetail.compareTo(zero) > 0 && allocateQtyOrderdetail.compareTo(zero) == 0
						&& pickQtyOrderdetail.compareTo(zero) == 0) {
					orderdetailStatus = OrderConstant.ORDERSTATUS_21;
				} else {
					// 只存在装车，明细的状态为已装车
					/*
					 * if (exists_11) { if (!exists_13 && !exists_9 && !exists_7 && !exists_5 &&
					 * !exists_1) { orderdetailStatus = OrderConstant.ORDERSTATUS_19; } else {
					 * orderdetailStatus = OrderConstant.ORDERSTATUS_18; } } else if (exists_9) { if
					 * (!exists_7 && !exists_5 && !exists_1) { orderdetailStatus =
					 * OrderConstant.ORDERSTATUS_15; } else { orderdetailStatus =
					 * OrderConstant.ORDERSTATUS_14; } } else if (exists_7) { if (!exists_5 &&
					 * !exists_1) { orderdetailStatus = OrderConstant.ORDERSTATUS_13; } else {
					 * orderdetailStatus = OrderConstant.ORDERSTATUS_11; } } else
					 */ if (exists_13) {
						if (!exists_9 && !exists_5) {
							orderdetailStatus = OrderConstant.ORDERSTATUS_13;
						} else {
							orderdetailStatus = OrderConstant.ORDERSTATUS_11;
						}
					} else if (exists_9) {
						if (!exists_5) {
							orderdetailStatus = OrderConstant.ORDERSTATUS_9;
						} else {
							orderdetailStatus = OrderConstant.ORDERSTATUS_7;
						}
					} else if (exists_5) {
						orderdetailStatus = OrderConstant.ORDERSTATUS_5;
					}

				}
			}

			// 更新明细的状态
			UpdateWrapper<OrderdetailEntity> orderdetailUpdate = new UpdateWrapper<OrderdetailEntity>();
			orderdetailUpdate.eq("orderkey", orderdetailEntity.getOrderkey());
			orderdetailUpdate.eq("line", orderdetailEntity.getLine());
			OrderdetailEntity orderdetailEntityUpdate = new OrderdetailEntity();
			orderdetailEntityUpdate.setStatus(orderdetailStatus);
			orderdetailDao.update(orderdetailEntityUpdate, orderdetailUpdate);
		}

	}

	public Integer getOrderdetaillistMaxLine(String orderkey, Integer line) {
		Integer count = 0;
		if (StringUtils.isNotBlank(orderkey)) {
			QueryWrapper<OrderdetaillistEntity> queryWrapper = new QueryWrapper<OrderdetaillistEntity>();
			queryWrapper.eq("orderkey", orderkey);
			queryWrapper.eq("line", line);
			queryWrapper.select(" isnull( max(linelist),0) maxline ");

			List<Map<String, Object>> getlist = baseDao.selectMaps(queryWrapper);
			if (getlist.size() > 0) {
				Map<String, Object> map = getlist.get(0);
				Object obj = map.get("maxline");
				if (obj instanceof Integer) {
					count = (Integer) obj;
				} else if (obj instanceof String) {
					count = Integer.valueOf((String) map.get("maxline"));
				}
			}
		}

		return count;
	}

	@Override
	public List<OrderdetaillistEntity> getByTray(String tray) {
		QueryWrapper<OrderdetaillistEntity> queryWrapper = new QueryWrapper<OrderdetaillistEntity>();
		queryWrapper.eq("lswms_orderdetaillist.tray", tray);
		queryWrapper.le("lswms_orderdetaillist.status", OrderConstant.ORDERSTATUS_13);

		queryWrapper.orderByAsc("lswms_orderdetaillist.sku", "line");
		return baseDao.selectListBySql(queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String savePicklist(List<OrderdetaillistEntity> list) {
		
		if(list.size()==0) {
			return "不存在分拣记录";
		}
		
		// 如果当前存在增在分配的订单，则不允许分配
		Integer countCurrnet = orderallocationDao.selectCount(null);
		if (countCurrnet > 0) {
			return MEGConstant.ORDER_HAS_OTHER_ALLOCATION;
		}
//		Result result = uniquecodeService.pickListUniquecode(list);
//
//		int code = result.getCode();
//		if(code!=0){
//			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//			return result.getMsg();
//
//		}
		// 若目的托盘和原托盘一样，则拣货数量要和箱数量一样
		// 如果不一致，则要更换新的托盘
		String tray = "";
		Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
		for (OrderdetaillistEntity orderdetaillistEntity : list) {
			BigDecimal qty = orderdetaillistEntity.getQty();
			String box = orderdetaillistEntity.getBox();
			String lot = orderdetaillistEntity.getLot();
			tray = orderdetaillistEntity.getTray();
			if (qty.compareTo(BigDecimal.ZERO) < 0) {
				return "数量不能为负数";
			}


			String key = lot + "#" + box;
			if (map.containsKey(key)) {
				map.put(key, map.get(key).add(qty));
			} else {
				map.put(key, qty);
			}
		}

		// 拣选后的数量与库存数量进行对比
		QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
		inventoryQuery.eq("tray", tray);
		inventoryQuery.gt("qty", 0);
		List<InventoryEntity> inventoryList = inventoryDao.selectList(inventoryQuery);
		for (InventoryEntity inventoryEntity : inventoryList) {
			BigDecimal qty = inventoryEntity.getQty();
			String lot = inventoryEntity.getLot();
			String box = inventoryEntity.getBox();

			String key = lot + "#" + box;
			if (map.containsKey(key)) {
				map.put(key, map.get(key).subtract(qty));
			} else {
				map.put(key, qty.negate());
			}
		};


		//校验有未完成的任务就不允许校验================================================================
		for (OrderdetaillistEntity orderdetaillistEntity : list){
			String tray2 = orderdetaillistEntity.getTray();
			if(tray2!=""&&tray2!=null){
				QueryWrapper<Integer> wrapper = new QueryWrapper<>();
				wrapper.eq("fromtray",tray2);
				wrapper.notIn("status",9);
				Integer task_tary = taskDao.getTaskTaryCount(wrapper);
				if(task_tary!=0){
					return "该箱存在未完成的任务，不能进行分拣！";
				}
			}
		}



		// 原托盘有剩余量，就需要分拣到新的托盘
		boolean mustChangeTray = false;
		for (BigDecimal qty : map.values()) {
			if (qty.compareTo(BigDecimal.ZERO) > 0) {
				return "库存不足，分拣失败";
			}
			if (qty.compareTo(BigDecimal.ZERO) < 0) {
				mustChangeTray = true;
			}
		}

		for (OrderdetaillistEntity orderdetaillistEntity : list) {
			BigDecimal qty = orderdetaillistEntity.getQty();
			// 填写的量
			BigDecimal actQty = orderdetaillistEntity.getActqty();
			tray = orderdetaillistEntity.getTray();
			String toTray = orderdetaillistEntity.getTotray();
			String box = orderdetaillistEntity.getBox();
			String toBox = orderdetaillistEntity.getTobox();
			String toloc = orderdetaillistEntity.getToloc();
			String sku = orderdetaillistEntity.getSku();
			String orderkey = orderdetaillistEntity.getOrderkey();
			Integer line = orderdetaillistEntity.getLine();
			Integer linelist = orderdetaillistEntity.getLinelist();
			String uniquecodes = orderdetaillistEntity.getUniquecodes();
			String isuniquecode = orderdetaillistEntity.getIsuniquecode();

//			if (actQty.compareTo(BigDecimal.ZERO) < 0) {
//				return "数量必须为正数";
//			}

			// 查询当前行的记录
			QueryWrapper<OrderdetaillistEntity> queryWrapper = new QueryWrapper<OrderdetaillistEntity>();
			queryWrapper.eq("orderkey", orderkey);
			queryWrapper.eq("line", line);
			queryWrapper.eq("linelist", linelist);

			OrderdetaillistEntity orderdetaillistEntityPre = baseDao.selectOne(queryWrapper);
			// 修改前的实际量
			BigDecimal actQtyPre = orderdetaillistEntityPre.getActqty();
			String lot = orderdetaillistEntityPre.getLot();
			String loc = orderdetaillistEntityPre.getLoc();
			Integer status = orderdetaillistEntityPre.getStatus();
			// 已拣货的明细不能在拣货
			if (status >= 13) {
				continue;
			}

			// 拣选数量与当前数量不一样时，提示需要更换托盘
			if (mustChangeTray && tray.equals(toTray)) {
				// 回滚事务
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return "请使用新的箱号";
			}
			// 如果是原数量且原tray，不允许换box
//			if (!mustChangeTray && tray.equals(toTray) && !box.equals(toBox)) {
//				// 回滚事务
//				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//				return "格号被改变，请还原";
//			}

			// 更换新的箱号后， 新箱号是否空闲，或新箱号只存pick货位有拣货记录，存放的是相同物料
			if (actQtyPre.compareTo(actQty) != 0 && tray.equals(toTray)) {
				// 查询新箱号的库存记录
				QueryWrapper<InventoryEntity> inventoryQueryTotray = new QueryWrapper<InventoryEntity>();
				inventoryQueryTotray.eq("tray", tray);
				inventoryQueryTotray.gt("qty", 0);

				List<InventoryEntity> inventoryListTotray = inventoryDao.selectList(inventoryQuery);

				for (InventoryEntity inventoryEntity : inventoryListTotray) {
					String inventorylocation = inventoryEntity.getLocation();
					if (!inventorylocation.equals("PICK")) {
						// 回滚事务
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						return "填写的箱号已被存储位使用，不允许参与分拣使用";
					}

					String inventoryBox = inventoryEntity.getBox();
					String inventorySku = inventoryEntity.getSku();
//					if (toBox.equals(inventoryBox) && !sku.equals(inventorySku)) {
//						// 回滚事务
//						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//						return "填写的托箱号已存有其他物料,请更换箱号";
//					}
//					if (toBox.equals(inventoryBox) && inventoryEntity.getQty().compareTo(BigDecimal.ZERO)>0 ) {
//						// 回滚事务
//						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//						return "填写的托格号已存放物料,请更换格号";
//					}
				}
			}

			//有时候传的唯一码字段的是null，会报空指针异常
			isuniquecode = isuniquecode==null ? "0" : isuniquecode;
			if (isuniquecode.equals("1")) {
				String[] split = uniquecodes.split("\n");
				for (String item : split) {
					Integer integer = uniquecodeDao.checkUniquecode(item);
					if (integer == 0) {
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						return "部分唯一码不存在";
					}
				}
				for (String item : split) {
					UniquecodeEntity uniquecodeEntity = uniquecodeDao.getListByUniquecode(item);
					String checkTray = uniquecodeEntity.getTray();
					String checkBox = uniquecodeEntity.getBox();
					if (!(checkBox.equals(box) && checkTray.equals(tray))) {
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						return  "该格没有对应的唯一码";
					}
				}
				
				if(actQty.compareTo(new BigDecimal(split.length))  !=0) {
					return  "箱："+tray +" 格"+box +"分拣的数量和唯一码数量不一致" ;
				}
				
				for (String item : split) {
					
					UniquecoderecordEntity uniquecoderecordEntity = new UniquecoderecordEntity();
					
					UniquecodeEntity uniquecodeET = uniquecodeDao.getListByUniquecode(item);
					
					uniquecoderecordEntity.setUniquecode(uniquecodeET.getUniquecode());
					uniquecoderecordEntity.setFromtray(uniquecodeET.getTray());
					uniquecoderecordEntity.setFrombox(uniquecodeET.getBox());
					uniquecoderecordEntity.setSku(uniquecodeET.getSku());
					uniquecoderecordEntity.setLot(uniquecodeET.getLot());
					
					uniquecodeET.setTray(toTray);
					uniquecodeET.setBox(toBox);
					
					uniquecoderecordEntity.setTotray(toTray);
					uniquecoderecordEntity.setTobox(toBox);
					uniquecoderecordEntity.setSourcetype(TradeConstant.TRADETYPE_MV);
					uniquecoderecordEntity.setSourcetable("orderdetaillist");
					uniquecoderecordEntity.setSourceid(orderdetaillistEntity.getId());
					uniquecoderecordEntity.setSourcekey(orderdetaillistEntity.getOrderkey());
					uniquecoderecordEntity.setSourceline(orderdetaillistEntity.getLine());
					uniquecoderecordEntity.setSourcelinelist(orderdetaillistEntity.getLinelist());
					
					uniquecodeDao.updateById(uniquecodeET);
					uniquecoderecordDao.insert(uniquecoderecordEntity) ;
				}
			}else if(isuniquecode.equals(null)){

			}

			// 原始托盘和箱号的记录qty减去实际的拣货量 、pickqty 减去原有的拣货量
			// 查询库存记录
			QueryWrapper<InventoryEntity> inventoryQuery2 = new QueryWrapper<InventoryEntity>();
			inventoryQuery2.eq("lot", lot);
			inventoryQuery2.eq("location", loc);
			inventoryQuery2.eq("tray", tray);
			inventoryQuery2.eq("box", box);
			InventoryEntity inventoryEntity = inventoryDao.selectOne(inventoryQuery2);

			// 对原托盘箱号库存记录做变更
			UpdateWrapper<InventoryEntity> inventoryUpdate = new UpdateWrapper<InventoryEntity>();
			inventoryUpdate.eq("lot", lot);
			inventoryUpdate.eq("location", loc);
			inventoryUpdate.eq("tray", tray);
			inventoryUpdate.eq("box", box);
			inventoryUpdate.set("qty", inventoryEntity.getQty().subtract(actQty));
			inventoryUpdate.set("pickqty", inventoryEntity.getPickqty().subtract(actQtyPre));
			inventoryDao.update(null, inventoryUpdate);

			// 对目的托盘箱号的库存记录做变更，若存在库存，更新记录，不存则新增记录
			QueryWrapper<InventoryEntity> inventoryToQuery = new QueryWrapper<InventoryEntity>();
			inventoryToQuery.eq("lot", lot);
			inventoryToQuery.eq("location", toloc);
			inventoryToQuery.eq("tray", toTray);
			inventoryToQuery.eq("box", toBox);
			InventoryEntity inventorytoEntity = inventoryDao.selectOne(inventoryToQuery);
			if (inventorytoEntity != null) {
				UpdateWrapper<InventoryEntity> inventoryToUpdate = new UpdateWrapper<InventoryEntity>();
				inventoryToUpdate.eq("lot", lot);
				inventoryToUpdate.eq("location", toloc);
				inventoryToUpdate.eq("tray", toTray);
				inventoryToUpdate.eq("box", toBox);
				inventoryToUpdate.set("qty", inventorytoEntity.getQty().add(actQty));
				inventoryToUpdate.set("pickqty", inventorytoEntity.getPickqty().add(actQty));
				inventoryDao.update(null, inventoryToUpdate);
			} else {
				InventoryEntity inventoryInsert = new InventoryEntity();
				inventoryInsert.setLot(lot);
				inventoryInsert.setSku(sku);
				inventoryInsert.setLocation(toloc);
				inventoryInsert.setTray(toTray);
				inventoryInsert.setBox(toBox);
				inventoryInsert.setQty(actQty);
				inventoryInsert.setPickqty(actQty);
				inventoryInsert.setPutqty(BigDecimal.ZERO);
				inventoryInsert.setStatus(0);
				inventoryDao.insert(inventoryInsert);
			}
			// 新增trade记录
			if (actQtyPre.compareTo(actQty) != 0 || !tray.equals(toTray) || !box.equals(toBox)) {
				// 增加交易信息
				String tradekey = sequenceService.getSequence("trade");
				TradeEntity tradeEntity = new TradeEntity();

				tradeEntity.setTradekey(tradekey);
				tradeEntity.setTradetype(TradeConstant.TRADETYPE_MV);
				tradeEntity.setSku(sku);
				tradeEntity.setQty(actQty);
				tradeEntity.setSourcetype(TradeConstant.SOURCETYPE_MOVE);
				tradeEntity.setSourcekey(null);
				tradeEntity.setSourceline(null);
				tradeEntity.setSourcelinelist(null);
				tradeEntity.setFromlocation(loc);
				tradeEntity.setTolocation(toloc);
				tradeEntity.setFromtray(tray);
				tradeEntity.setTotray(toTray);
				tradeEntity.setFrombox(box);
				tradeEntity.setTobox(toBox);
				tradeEntity.setFromlot(lot);
				tradeEntity.setTolot(lot);
				tradeEntity.setFromlot1(null);
				tradeEntity.setFromlot2(null);
				tradeEntity.setFromlot3("");
				tradeEntity.setFromlot4("");
				tradeEntity.setFromlot5("");
				tradeEntity.setFromlot6("");
				tradeEntity.setFromlot7("");
				tradeEntity.setFromlot8("");
				tradeEntity.setFromlot9("");
				tradeEntity.setFromlot10("");
				tradeEntity.setFromlot11("");
				tradeEntity.setFromlot12("");
				tradeEntity.setFromlot13("");
				tradeEntity.setFromlot14("");
				tradeEntity.setFromlot15("");
				tradeEntity.setTolot1(null);
				tradeEntity.setTolot2(null);
				tradeEntity.setTolot3("");
				tradeEntity.setTolot4("");
				tradeEntity.setTolot5("");
				tradeEntity.setTolot6("");
				tradeEntity.setTolot7("");
				tradeEntity.setTolot8("");
				tradeEntity.setTolot9("");
				tradeEntity.setTolot10("");
				tradeEntity.setTolot11("");
				tradeEntity.setTolot12("");
				tradeEntity.setTolot13("");
				tradeEntity.setTolot14("");
				tradeEntity.setTolot15("");
				tradeDao.insert(tradeEntity);
			}

			// 更改当前行的状态和数量
			UpdateWrapper<OrderdetaillistEntity> orderdetaillistUpdate = new UpdateWrapper<OrderdetaillistEntity>();
			orderdetaillistUpdate.eq("orderkey", orderkey);
			orderdetaillistUpdate.eq("line", line);
			orderdetaillistUpdate.eq("linelist", linelist);

			OrderdetaillistEntity orderdetaillistEntityUpdate = new OrderdetaillistEntity();
			orderdetaillistEntityUpdate.setStatus(OrderConstant.ORDERSTATUS_13);
			orderdetaillistEntityUpdate.setTray(toTray);
			orderdetaillistEntityUpdate.setBox(toBox);
			orderdetaillistEntityUpdate.setTotray(toTray);
			orderdetaillistEntityUpdate.setTobox(toBox);
			orderdetaillistEntityUpdate.setActqty(actQty);
			baseDao.update(orderdetaillistEntityUpdate, orderdetaillistUpdate);

			// 更改订单明细的记录 分配量减去actQtyPre ，拣货量增加 actQty
			QueryWrapper<OrderdetailEntity> orderdetailQuery = new QueryWrapper<OrderdetailEntity>();
			orderdetailQuery.eq("orderkey", orderkey);
			orderdetailQuery.eq("line", line);
			OrderdetailEntity orderdetailEntity = orderdetailDao.selectOne(orderdetailQuery);

			OrderdetailEntity orderdetailEntityUpdate = new OrderdetailEntity();
			orderdetailEntityUpdate.setAllocateqty(orderdetailEntity.getAllocateqty().subtract(actQtyPre));
			orderdetailEntityUpdate.setPickqty(orderdetailEntity.getPickqty().add(actQty));
			UpdateWrapper<OrderdetailEntity> orderdetailUpdate = new UpdateWrapper<OrderdetailEntity>();
			orderdetailUpdate.eq("orderkey", orderkey);
			orderdetailUpdate.eq("line", line);
			orderdetailDao.update(orderdetailEntityUpdate, orderdetailUpdate);

			// 更新状态
//			changeOrderdetailByOrderdetaillist(orderkey);
			changeOrderStatus(orderkey);
		}

		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String backToStereo(String tray) {
		// 检查托盘是否在拣选位是否有库存
		QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
		inventoryQuery.eq("tray", tray);
		inventoryQuery.gt("qty", 0);

		List<InventoryEntity> list = inventoryDao.selectList(inventoryQuery);
		if (list.size() == 0) {
			return "箱："+tray+"为空，不能回库";
		}
		for (InventoryEntity inventoryEntity : list) {
			BigDecimal pickQty = inventoryEntity.getPickqty();
			BigDecimal putQty = inventoryEntity.getPutqty();
			if (pickQty.compareTo(BigDecimal.ZERO) > 0 || putQty.compareTo(BigDecimal.ZERO) > 0) {
				return "箱："+tray+"存在其它未完成的任务，不能回库";
			}

//			String location = inventoryEntity.getLocation();
//			if (!location.equalsIgnoreCase("PICK")) {
//				return "箱："+tray+"不在拣选位，不能回库";
//			}
		}

		QueryWrapper<TaskEntity> taskQuery = new QueryWrapper<TaskEntity>();

		taskQuery.lt("status", TaskConstant.TASK_9);
		taskQuery.and(wrapper -> wrapper.eq("fromtray", tray).or().eq("totray", tray));
		Integer count = taskDao.selectCount(taskQuery);
		if (count > 0) {
			return "箱："+tray+"存在其它未完成的任务，不能回库";
		}
		for (InventoryEntity item : list) {
			receiptdetaillistService.getPutawayTask(item,"");
		}

		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String deletePick(Long[] ids) {
		QueryWrapper<OrderdetaillistEntity> orderdetaillistQuery = new QueryWrapper<OrderdetaillistEntity>();
		orderdetaillistQuery.in("id", ids);
		List<OrderdetaillistEntity> orderdetaillists = baseDao.selectList(orderdetaillistQuery);
		for (OrderdetaillistEntity orderdetaillistEntity : orderdetaillists) {
			Integer status = orderdetaillistEntity.getStatus();
			if (status.equals(OrderConstant.ORDERSTATUS_21)) {
				return "拣货明细已发货，无法删除";
			}
		}

		for (OrderdetaillistEntity orderdetaillistEntity : orderdetaillists) {
			Integer status = orderdetaillistEntity.getStatus();
			// 变更订单明细的数量
			QueryWrapper<OrderdetailEntity> orderdetailQuery = new QueryWrapper<OrderdetailEntity>();
			orderdetailQuery.eq("orderkey", orderdetaillistEntity.getOrderkey());
			orderdetailQuery.eq("line", orderdetaillistEntity.getLine());
			OrderdetailEntity orderdetailEntity = orderdetailDao.selectOne(orderdetailQuery);
			// 设置明细的数量

			OrderdetailEntity orderdetailEntityUpdate = new OrderdetailEntity();

//			if (status == OrderConstant.ORDERSTATUS_0 || status == OrderConstant.ORDERSTATUS_9) {
			if (status <= OrderConstant.ORDERSTATUS_9) {
				orderdetailEntityUpdate
						.setAllocateqty(orderdetailEntity.getAllocateqty().subtract(orderdetaillistEntity.getActqty()));
			} else {
				orderdetailEntityUpdate
						.setPickqty(orderdetailEntity.getPickqty().subtract(orderdetaillistEntity.getActqty()));
			}
			UpdateWrapper<OrderdetailEntity> orderdetailUpdate = new UpdateWrapper<OrderdetailEntity>();
			orderdetailUpdate.eq("orderkey", orderdetaillistEntity.getOrderkey());
			orderdetailUpdate.eq("line", orderdetaillistEntity.getLine());
			orderdetailDao.update(orderdetailEntityUpdate, orderdetailUpdate);

			// 变更库存 查询库存记录，更改拣货数量
			QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
			inventoryQuery.eq("lot", orderdetaillistEntity.getLot());
			inventoryQuery.eq("location", orderdetaillistEntity.getLoc());
			inventoryQuery.eq("tray", orderdetaillistEntity.getTray());
			inventoryQuery.eq("box", orderdetaillistEntity.getBox());
			inventoryQuery.eq("sku", orderdetaillistEntity.getSku());
			InventoryEntity inventoryEntity = inventoryDao.selectOne(inventoryQuery);

			// 更改库存记录
			InventoryEntity inventoryEntityUpdate = new InventoryEntity();
			inventoryEntityUpdate.setPickqty(inventoryEntity.getPickqty().subtract(orderdetaillistEntity.getActqty()));

			UpdateWrapper<InventoryEntity> inventoryUpdate = new UpdateWrapper<InventoryEntity>();
			inventoryUpdate.eq("lot", orderdetaillistEntity.getLot());
			inventoryUpdate.eq("location", orderdetaillistEntity.getLoc());
			inventoryUpdate.eq("tray", orderdetaillistEntity.getTray());
			inventoryUpdate.eq("box", orderdetaillistEntity.getBox());
			inventoryUpdate.eq("sku", orderdetaillistEntity.getSku());
			inventoryDao.update(inventoryEntityUpdate, inventoryUpdate);

			// 删除记录

			baseDao.deleteById(orderdetaillistEntity.getId());

			changeOrderStatus(orderdetaillistEntity.getOrderkey());
		}

		return null;

	}

	@Override
	public String updatePick(OrderdetaillistDTO dto) {
		QueryWrapper<OrderdetaillistEntity> orderdetaillistQuery = new QueryWrapper<OrderdetaillistEntity>();
		orderdetaillistQuery.eq("orderkey", dto.getOrderkey());
		orderdetaillistQuery.eq("line", dto.getLine());
		orderdetaillistQuery.eq("linelist", dto.getLinelist());

		OrderdetaillistEntity orderdetaillistEntity = baseDao.selectOne(orderdetaillistQuery);

		Integer status = orderdetaillistEntity.getStatus();
		if (!status.equals(OrderConstant.ORDERSTATUS_0)) {
			return "拣货明细不是新建状态，无法修改";
		}

		// 录入的数量
		BigDecimal qty = dto.getQty();
		if (qty.compareTo(BigDecimal.ZERO) <= 0) {
			return "拣货明细数量必须为正数";
		}

		// 变更订单明细的数量
		QueryWrapper<OrderdetailEntity> orderdetailQuery = new QueryWrapper<OrderdetailEntity>();
		orderdetailQuery.eq("orderkey", orderdetaillistEntity.getOrderkey());
		orderdetailQuery.eq("line", orderdetaillistEntity.getLine());
		OrderdetailEntity orderdetailEntity = orderdetailDao.selectOne(orderdetailQuery);
		// 设置明细的数量

		OrderdetailEntity orderdetailEntityUpdate = new OrderdetailEntity();

		if (status == OrderConstant.ORDERSTATUS_0 || status == OrderConstant.ORDERSTATUS_9) {
			orderdetailEntityUpdate.setAllocateqty(
					orderdetailEntity.getAllocateqty().subtract(orderdetaillistEntity.getActqty()).add(qty));
		} else {
			orderdetailEntityUpdate
					.setPickqty(orderdetailEntity.getPickqty().subtract(orderdetaillistEntity.getActqty()).add(qty));
		}
		UpdateWrapper<OrderdetailEntity> orderdetailUpdate = new UpdateWrapper<OrderdetailEntity>();
		orderdetailUpdate.eq("orderkey", orderdetaillistEntity.getOrderkey());
		orderdetailUpdate.eq("line", orderdetaillistEntity.getLine());
		orderdetailDao.update(orderdetailEntityUpdate, orderdetailUpdate);

		// 变更库存 查询库存记录，更改拣货数量
		QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
		inventoryQuery.eq("lot", orderdetaillistEntity.getLot());
		inventoryQuery.eq("location", orderdetaillistEntity.getLoc());
		inventoryQuery.eq("tray", orderdetaillistEntity.getTray());
		inventoryQuery.eq("box", orderdetaillistEntity.getBox());
		inventoryQuery.eq("sku", orderdetaillistEntity.getSku());
		InventoryEntity inventoryEntity = inventoryDao.selectOne(inventoryQuery);

		// 更改库存记录
		InventoryEntity inventoryEntityUpdate = new InventoryEntity();
		inventoryEntityUpdate
				.setPickqty(inventoryEntity.getPickqty().subtract(orderdetaillistEntity.getActqty()).add(qty));

		UpdateWrapper<InventoryEntity> inventoryUpdate = new UpdateWrapper<InventoryEntity>();
		inventoryUpdate.eq("lot", orderdetaillistEntity.getLot());
		inventoryUpdate.eq("location", orderdetaillistEntity.getLoc());
		inventoryUpdate.eq("tray", orderdetaillistEntity.getTray());
		inventoryUpdate.eq("box", orderdetaillistEntity.getBox());
		inventoryUpdate.eq("sku", orderdetaillistEntity.getSku());
		inventoryDao.update(inventoryEntityUpdate, inventoryUpdate);

		// 更改记录数据

		// baseDao.deleteById(orderdetaillistEntity.getId());
		UpdateWrapper<OrderdetaillistEntity> orderdetaillistWrapper = new UpdateWrapper<OrderdetaillistEntity>();
		orderdetaillistWrapper.eq("orderkey", dto.getOrderkey());
		orderdetaillistWrapper.eq("line", dto.getLine());
		orderdetaillistWrapper.eq("linelist", dto.getLinelist());

		OrderdetaillistEntity orderdetaillistEntityUpate = new OrderdetaillistEntity();
		orderdetaillistEntityUpate.setActqty(qty);

		baseDao.update(orderdetaillistEntityUpate, orderdetaillistWrapper);

		changeOrderStatus(orderdetaillistEntity.getOrderkey());

		return null;
	}

	

}
